package org.soenea.doitall.application.dispatcher;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.dsrg.soenea.application.servlet.dispatcher.Dispatcher;

public abstract class BuddyAgeDispatcher extends Dispatcher {
	/**
	 * Forwards to "/WEB-INF/JSP/" + getFormat() + "/" + target.
	 */
	protected void forwardJSP(String target) throws IOException,
			ServletException {
		super.forward("/WEB-INF/JSP/" + getFormat() + "/" + target);
	}

	protected String getFormat() {
		final String format = this.myHelper.getString("format");
		return (format == null || !"XML".equals(format.toUpperCase())) ? "HTML"
				: format.toUpperCase();
	}

	@Override
	public void init(HttpServletRequest req, HttpServletResponse res) {
		super.init(req, res);
	}

	/**
	 * Redirects to: <context-path><servlet-path>/p
	 * 
	 * @param p
	 *            the last part of the path to be redirected to.
	 * @throws IOException
	 */
	public void relativeRedirect(String p) throws IOException {
		final String path = this.myRequest.getContextPath()
				+ this.myRequest.getServletPath() + "/" + p;
		this.myResponse.sendRedirect(path);
	}
}
